
import { uuidv4 } from '@antv/xflow'
import FengJinIcon from '@/assets/svg/FengJin'
import { NODE_TYPE_COLOR, NODE_TYPE_RENDER_KEY, NODE_TYPE } from '@/constants'
const formatNode = data => {
  let res = data.map(app => {
    const children = app.script.commands.map(command => {
      const { properties, propertyOrder, required } = command.input
      let globalVariables: string[] = []
      const form = propertyOrder.map(key => {
        const property = properties[key]
        let formItem = {
          label: property.name,
          field: key,
          required: required.includes(key),
          // type: 'Select'
          type: 'Input'
        }
        if (property.value_mode === '0') {
          if (property.input_mode === '0') {
            formItem.type = 'Input'
          } else {
            formItem.type = 'Select'
            formItem.options = property.preset_input.map(itm => ({
              label: itm,
              value: itm
            }))
          }
        } else {
          formItem.type = 'Cascader'
          formItem.options = []
          globalVariables.push(key)
        }
        return formItem
      })
      const nodeInfo = {
        nodeType: NODE_TYPE.NORMAL,
        integration_id: app.id,
        display: command.display,
        name: command.name,
        tags: [],
        description: command.description,
        // category: '分类一',
        // script: '这是一段脚本',
        input: {
          form,
          value: {}
        },
        output: command.output.propertyOrder.map(property => ({
          label: command.output.properties[property].name,
          value: property,
          type: command.output.properties[property].type
        })),
        globalVariables
      }
      return {
        key: uuidv4(),
        label: command.display,
        value: command.name,
        data: nodeInfo,
        icon: <FengJinIcon style={{ marginRight: 12 }} />,
        renderKey: NODE_TYPE_RENDER_KEY[command.display] || 'AUTO_BAN',
        width: 200,
        height: 36
      }
    })
    return {
      key: uuidv4(),
      label: (
        <span style={{ color: NODE_TYPE_COLOR[app.display] }}>
          {app.display}
        </span>
      ),
      value: app.name,
      children
    }
  })
  const switchNodes = {
    key: uuidv4(),
    label: (
      <span style={{ color: NODE_TYPE_COLOR['数据分流'] }}>
        数据分流
      </span>
    ),
    value: 'shunt',
    children: [
      {
        key: uuidv4(),
        label: '数据判断',
        value: 'switch',
        data: {
          nodeType: NODE_TYPE.SWITCH,
          display: '数据判断',
          input: {
            form: [
              {
                label: '判断变量',
                field: 'cnd',
                required: true,
                type: 'Cascader',
                options: []
              }
            ],
            value: {}
          },
          globalVariables: ['cnd']
        },
        icon: <FengJinIcon style={{ marginRight: 12 }} />,
        renderKey: NODE_TYPE_RENDER_KEY['数据判断'],
        width: 200,
        height: 72
      }
    ]
  }
  console.log([...res, switchNodes]);
  
  return [...res, switchNodes]
}

export { formatNode }
